分词
将文本按 word / sentence / paragraph / document 粒度切分。
函数
NaturalLanguage.tokenize(text, options?): TokenRange[]
按指定粒度遍历 text,返回按文本顺序排列的 token 列表。
options.unit?: "word" | "sentence" | "paragraph" | "document"—— 默认"word"。options.language?: Language—— 可选语言提示,对没有显式词边界的语言(中文、日文、泰文等)尤其有用。
每个 token 形如:
示例
注意事项
range.location/range.length是 UTF-16 偏移,与 JS 原生字符串索引一致。text.substring(range.location, range.location + range.length)一定还原token.text。- 分词器不会过滤标点或空白。如果只想拿到有意义的词,自行过滤或改用
Tagger.tags(..., { omitPunctuation: true, omitWhitespace: true })。
